package net.zio.kamenriderzioaddon.ridersystem.behavior.impl;

import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.zio.kamenriderzioaddon.ridersystem.behavior.IAwakeningHenshinBehavior;
import net.zio.kamenriderzioaddon.ridersystem.data.PlayerDataManager;
import net.zio.kamenriderzioaddon.ridersystem.registry.AwakeningConfig;
import net.zio.kamenriderzioaddon.ridersystem.registry.FullRiderConfig;

import net.zio.kamenriderzioaddon.enums.HenshinState;


/**
 * 默认强化形态变身行为实现
 */
public class DefaultAwakeningHenshinBehavior implements IAwakeningHenshinBehavior {

    @Override
    public void handleAwakeningHenshin(Player player, FullRiderConfig fullRiderConfig, AwakeningConfig awakeningConfig) {
        // 1. 检查是否处于基础变身状态
        //HenshinUtils.sendMessage(player, "启动强化形态变身: " + awakeningConfig.getName());

        // 2. 装备强化形态装备（不保存原有装备）
        equipAwakeningArmorSet(player, awakeningConfig);

        // 3. 播放音效等
        //HenshinUtils.playSound(player, baseRiderConfig.getSoundMap().get(SoundType.AWAKENING));

        // 4. 更新状态为强化形态激活
        PlayerDataManager data = PlayerDataManager.get(player);
        //data.setRiderState(awakeningConfig, HenshinState.ACTIVE_AWAKENING);
        data.setActiveRiderName(awakeningConfig.getName());
    }

    /**
     * 处理强化形态解除
     *
     * @param player 玩家实例
     * @param fullRiderConfig 基础骑士配置
     * @param awakeningConfig 强化形态配置
     */
    @Override
    public void handleAwakeningDeactivation(Player player,  FullRiderConfig fullRiderConfig, AwakeningConfig awakeningConfig) {
        //HenshinUtils.sendMessage(player, "解除强化形态: " + awakeningConfig.getName());

//        // 1. 解除强化形态装备
//        unequipAwakeningArmorSet(player, awakeningConfig);
//
//        // 2. 恢复基础形态装备
//        fullRiderConfig.equipFullArmorSet(player);
//
//        // 3. 更新状态为基础形态激活
//        PlayerDataManager data = PlayerDataManager.get(player);
//        data.setRiderState(fullRiderConfig, HenshinState.ACTIVE);
//        data.setActiveRiderName("");

    }

    /**
     * 处理 standby 形态
     * @param player 玩家实例
     * @param fullRiderConfig 完整骑士配置
     * @param awakeningConfig 强化形态配置
     */
    @Override
    public void handleStandbyAwakening(Player player, FullRiderConfig fullRiderConfig, AwakeningConfig awakeningConfig) {
        // 获取 PlayerDataManager 实例
        PlayerDataManager dataManager = PlayerDataManager.get(player);

        // 1. 标记变身中
        dataManager.setHenshining(true);
        dataManager.sync(player);
    }

    @Override
    public void handleQuickTransformation(Player player, FullRiderConfig fullRiderConfig, AwakeningConfig awakeningConfig) {

    }


    private void equipAwakeningArmorSet(Player player, AwakeningConfig awakeningConfig) {
        EquipmentSlot[] slots = {
                EquipmentSlot.HEAD,
                EquipmentSlot.CHEST,
                EquipmentSlot.LEGS,
                EquipmentSlot.FEET
        };

        Item[] armorSet = awakeningConfig.getArmorSet();
        for (int i = 0; i < slots.length; i++) {
            player.setItemSlot(slots[i], new ItemStack(armorSet[i]));
        }
    }

    private void unequipAwakeningArmorSet(Player player, AwakeningConfig awakeningConfig) {
        EquipmentSlot[] slots = {
                EquipmentSlot.HEAD,
                EquipmentSlot.CHEST,
                EquipmentSlot.LEGS,
                EquipmentSlot.FEET
        };

        for (EquipmentSlot slot : slots) {
            player.setItemSlot(slot, ItemStack.EMPTY);
        }
    }
}